<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        div {
            width: 200px;
            height: 50px;
            display: flex;
            margin: 50px;
        }

        div>input {
            width: 100px;
            text-align: center;
            font-size: 20px;
        }

        div>button {
            width: 50px;
            height: 50px;
            font-size: 22px;
        }
    </style>
</head>

<body>

    <div>
        <button class="sub">-</button>
        <input type="text" value="1">
        <button class="add">+</button>
    </div>

    <div>
        <button class="sub2">-</button>
        <input class="second" type="text" value="1">
        <button class="add2">+</button>
    </div>




    <script>

        function outer() {
            let count = 1;
            return {
                get fn1() { return count }, // 获取输入框的值
                set fn2(val) { count = val } // 改变输入框的值
            }
        }



        const subbtn = document.querySelector('.sub');
        const addbtn = document.querySelector('.add');
        const inpvalue = document.querySelector('input');
        const n1 = outer();
        subbtn.onclick = function () {
            const res = n1.fn1;  //获取输入框中的数据
            if(res<=1) res=1;
            n1.fn2 = res-1;
            inpvalue.value =  n1.fn1;
        }


        addbtn.onclick = function () {
            const res = n1.fn1;  //获取输入框中的数据
            n1.fn2 = res+1;
            inpvalue.value =  n1.fn1;
        }



        const subbtn2 = document.querySelector('.sub2');
        const addbtn2 = document.querySelector('.add2');
        const inpvalue2 = document.querySelector('.second');
        const n2 = outer();
        subbtn2.onclick = function () {
            const res = n2.fn1;  //获取输入框中的数据
            if(res<=1) res=1;
            n2.fn2 = res-1;
            
            inpvalue.value =  n2.fn1;
        }


        addbtn2.onclick = function () {
            const res = n2.fn1;  //获取输入框中的数据
            n2.fn2 = res+1;
            inpvalue.value =  n2.fn1;
        }


    </script>
</body>

</html>